<template>
  <div class="login">
    <el-card shadow="always">
      <el-form status-icon :rules="rules" :model="ruleForm" ref="ruleForm">
        <el-form-item>
          <div class="title"></div>
        </el-form-item>
        <el-form-item prop="username">
          <el-input placeholder="请输入用户名" v-model="ruleForm.username" clearable> </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" v-model="ruleForm.password" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">登录</el-button>
        </el-form-item>
      </el-form>
      <el-link type="primary" @click="$router.push('/reg')">没有账号?去注册</el-link>
    </el-card>
  </div>
</template>

<script>
import { loginAPI } from '../axios/loginAPI.js'
export default {
  data() {
    return {
      ruleForm: {
        username: 'lijianyu',
        password: '123456'
      },

      rules: {
        username: [{ required: true, pattern: /^[a-zA-Z0-9]{1,10}$/, message: '字母数字、长度 1-10', trigger: 'blur' }],
        password: [
          { required: true, pattern: /^[a-zA-Z0-9]{6,15}$/, message: '非空字符串、长度 6-15', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    onSubmit() {
      this.$refs.ruleForm.validate(async valid => {
        if (!valid) return
        // console.log(123)
        const { data: res } = await loginAPI(this.ruleForm)
        console.log(res)
        if (res.code === 0) {
          this.$message({
            message: res.message,
            type: 'success'
          })
          //   this.ruleForm.username = ''
          //   this.ruleForm.password = ''
          // 存token
          this.$store.commit('user/updataToken', res.token)
          this.$router.push('/')
        } else {
          this.$message({
            message: res.message,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>

<style lang="less">
.login {
  height: 100%;
  background: url('../assets/images/login_bg.jpg') no-repeat;
  background-position: center;
  background-size: cover;
  .el-card {
    background-color: #fff;
    width: 350px;
    min-height: 200px;
    position: relative;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .title {
      height: 30px;
      background: url('../assets/images/login_title.png');
      background-repeat: no-repeat;
      background-position: center;
    }
    .el-form {
      //   display: flex;
      //   align-items: center;
      .el-button {
        width: 100%;
      }
    }
  }
}
</style>
